/*
 * File:     boardwidget.cpp
 * Author:   Thomas Bruederli
 *
 * Implementation of the QWidget to display the status of the balance board.
 *
 * This file is part of the Qt-based cross-platform Wii device driver.
 *
 * Copyright (C) 2009-2010, Dominic Stalder & Thomas Bruederli
 * Licensed under the GNU Lesser General Public License (LGPL) as published
 * by the Free Software Foundation at http://www.gnu.org/licenses/lgpl-3.0.txt
 *
 * $Id: boardwidget.cpp 2 2010-02-15 12:48:56Z roundcube $
 */

#include <QtGui>
#include "boardwidget.h"

BoardWidget::BoardWidget(QWidget *parent) : QWidget(parent)
{
	centerx = 236;
	centery = 168;
	
	resize(470, 330);
	setMinimumSize(470, 330);
	
    statusLabel = new QLabel(this);
    statusLabel->setGeometry(140, 20, 300, 20);
    QFont font;
    font.setBold(true);
    font.setWeight(75);
    statusLabel->setFont(font);
    statusLabel->setAlignment(Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter);
	
	label1 = new QLabel(this);
    label1->setText(tr("Instructions:"));
    label1->setGeometry(QRect(40, 20, 90, 20));
    label1->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);	

	bgimage = new QLabel(this);
	bgimage->setPixmap(QPixmap(":/res/wiiboard.png"));
	bgimage->setGeometry(70, 65, 334, 212);
	
	masspoint = new QLabel(this);
	masspoint->setPixmap(QPixmap(":/res/greendot.png"));
	masspoint->setGeometry(centerx - 12, centery - 12, 24, 24);
		
    weightTopLeft = new QLineEdit(this);
    weightTopLeft->setGeometry(50, 70, 70, 22);
    weightTopLeft->setAlignment(Qt::AlignCenter);
    weightTopLeft->setReadOnly(true);
					
    weightTopRight = new QLineEdit(this);
    weightTopRight->setGeometry(350, 70, 70, 22);
    weightTopRight->setAlignment(Qt::AlignCenter);
    weightTopRight->setReadOnly(true);
					
    weightBottomLeft = new QLineEdit(this);
    weightBottomLeft->setGeometry(50, 245, 70, 22);
    weightBottomLeft->setAlignment(Qt::AlignCenter);
    weightBottomLeft->setReadOnly(true);
					
    weightBottomRight = new QLineEdit(this);
    weightBottomRight->setGeometry(350, 245, 70, 22);
    weightBottomRight->setAlignment(Qt::AlignCenter);
    weightBottomRight->setReadOnly(true);

    totalWeight = new QLineEdit(this);
    totalWeight->setGeometry(200, 280, 70, 22);
    totalWeight->setAlignment(Qt::AlignCenter);
    totalWeight->setReadOnly(true);

    label2 = new QLabel(this);
	label2->setText(tr("Weight:"));
    label2->setGeometry(100, 280, 90, 20);
    label2->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
	
	setValues(0.0, 0.0, 0.0, 0.0, 0.0);
}

void BoardWidget::setInstructions(int, const QString &msg, bool)
{
	statusLabel->setText(msg);
}

void BoardWidget::setValues(float tl, float tr, float br, float bl, float weight)
{
	// write values to UI elements
	weightTopLeft->setText(QString("%1").arg((double)tl, 0, 'f', 1));
	weightTopRight->setText(QString("%1").arg((double)tr, 0, 'f', 1));
	weightBottomLeft->setText(QString("%1").arg((double)bl, 0, 'f', 1));
	weightBottomRight->setText(QString("%1").arg((double)br, 0, 'f', 1));
	totalWeight->setText(QString("%1").arg((double)weight, 0, 'f', 1));
	
	int offx = centerx;
	int offy = centery;
	
	// compute mass vector
	if (weight > 0) {
		double dx = ((tr+br) - (tl+bl)) / weight;
		double dy = ((bl+br) - (tl+tr)) / weight;
		offx += (int)(dx * 120);
		offy += (int)(dy * 65);
	}
	
	// move mass point icon
	masspoint->setGeometry(offx - 12, offy - 12, 24, 24);
	masspoint->setVisible(weight > 0);
}

void BoardWidget::buttonChanged(int which, int on)
{
	// TODO: Display button status
}

